﻿using System;
using System.Collections.Generic;
using System.Data.Linq.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using Bias_Stock.Models;
using System.Reflection;
using System.Data.Entity;
using System.Collections;
using WinNemoLib;

namespace Bias_Stock.Controllers
{
    public class MasterController : Controller
    {
        //
        // GET: /Master/

        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult add_Cart(List<MasterModel.CartMast_DS> theData)
        {
            MasterModel.CartMast_DS data = theData[0];
             MasterModel ins = new MasterModel();

            List<MasterModel.CartMast_DS> list = ins.SEL_Cart_MASTER(data._Code.ToString());
            if (list.Count > 0)
            {
                 return Json(new { success = 0, message = "DATA EXIT" });
            }
            using (ConnectionWrapper cnn = WinNemoLib.Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
                cnn.Open();
                cnn.BeginTransaction();
                data._ModifyBy = User.Identity.Name.ToString();
               
                if (ins.Ins_Data_Cart(cnn, data)==false) 
                {
                    return Json(new { success = 1, message = "CANNOT INSERT" });
                }
                cnn.CommitTransaction();
               
            }

            return Json(new { success = 1, message = "INSERT COMPELTE" });
        }
        [HttpPost]
        public JsonResult Edit_Cart(List<MasterModel.CartMast_DS> theData)
        {
            MasterModel.CartMast_DS data = theData[0];
            using (ConnectionWrapper cnn = WinNemoLib.Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
                cnn.Open();
                cnn.BeginTransaction();
                data._ModifyBy = User.Identity.Name.ToString();
                MasterModel ins = new MasterModel();
                if (ins.Edit_Data_Cart(cnn, data) == false)
                {
                    return Json(new { success = 1, message = "CANNOT EDIT" });
                }
                cnn.CommitTransaction();

            }

            return Json(new { success = 1, message = "EDIT COMPELTE" });
        }

       
        [HttpPost]
        public JsonResult CartList()
        {
            string page = Request["page"];
            string perpage = Request["rp"];
            string qtype = Request.Form["qtype"].ToString();
            string sortBy = Request.Form["sortname"].ToString();
            string sortorder = Request.Form["sortorder"].ToString();

            int start = (Convert.ToInt32(page) - 1) * Convert.ToInt32(perpage);

            MasterModel rs = new MasterModel();
            List<MasterModel.CartMast_DS> list = rs.SEL_Cart_MASTER("");    
 
            if (sortorder == "asc")
            {
                list = list .OrderBy(r => r.GetType().GetProperty(sortBy).GetValue(r, null)).ToList();
            }
            if (sortorder == "desc")
            {
                list = list.OrderByDescending(r => r.GetType().GetProperty(sortBy).GetValue(r, null)).ToList();
            }
            var wOrder = from c in list select new { c._Code, c._NotUse, c._Type,c._ModifyBy,c._ModifyDT };
            List<MasterModel.CartMast_DS> list2 = list.Skip(Convert.ToInt32(page)-1 * Convert.ToInt32(perpage)).Take(Convert.ToInt32(perpage)).ToList();

            Flexigrid.FlexigridCart_Master flexigridObject = new Flexigrid.FlexigridCart_Master();
            flexigridObject.page = 1;
            flexigridObject.total = list.Count();
            flexigridObject.rows = list2;
            return Json(flexigridObject);
        }

     
    }
   
}
